package com.xbzl.cly_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbzl.cly_demo.entity.Vehicle;
import com.xbzl.cly_demo.entity.VehicleAbnormalRecords;
import com.xbzl.cly_demo.exception.VehicleNotFoundException;
import com.xbzl.cly_demo.mapper.VehicleAbnormalRecordsMapper;
import com.xbzl.cly_demo.mapper.VehicleStatusMapper;
import com.xbzl.cly_demo.service.VehicleAbnormalRecordsService;
import com.xbzl.cly_demo.service.VehicleService;
import com.xbzl.cly_demo.mapper.VehicleMapper;
import com.xbzl.cly_demo.vo.VehicleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 26586
* @description 针对表【vehicle(车辆基本信息表)】的数据库操作Service实现
* @createDate 2025-04-03 15:23:40
*/
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle>
    implements VehicleService{
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    VehicleAbnormalRecordsMapper vehicleAbnormalRecordsMapper;

    @Override
    public VehicleVO queryVehicleByVin(String vehicleVin) {
        Vehicle vehicle = vehicleMapper.selectOne(new QueryWrapper<Vehicle>()
                .eq("vin", vehicleVin));
        if (vehicle == null){
            throw new VehicleNotFoundException("车辆不存在");
        }

        List<VehicleAbnormalRecords> vehicleAbnormalRecords = vehicleAbnormalRecordsMapper
                .selectList(new QueryWrapper<VehicleAbnormalRecords>()
                .eq("vehicle_id", vehicle.getVehicleId()));

        return VehicleVO.builder()
                .vehicleVin(vehicle.getVin())
                .plateNo(vehicle.getPlateNo())
                .deviceName(vehicle.getDeviceName())
                .vehicleAbnormalRecords(vehicleAbnormalRecords)
                .build();
    }
}




